import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';
import 'dart:io';

//将根路由设置为 /test
//运行 flutter driver --target=test_driver/app.dart
void main() {
  group('App', (){
    late FlutterDriver driver;
    Display display = Display(stdout);

    final actionChip = find.byValueKey('actionChip');
    final actionChipLabelText = find.byValueKey('actionChipLabelText');

    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

    tearDownAll(() async {
      if(driver != null){
        driver.close();
      }
    });

    test('start at 0', () async{
      if(driver != null){
          //打印
          display.myPrint('hello world ${await driver.getText(actionChipLabelText)}');
          expect(await driver.getText(actionChipLabelText), '0');
      }
      
    });

    test('increments the counter', () async{
      await driver.tap(actionChip);

      expect(await driver.getText(actionChipLabelText), '1');
    });

  });
}

class Display{
  late IOSink output;
  late String lastTextPrinted;

  Display(IOSink output){
    this.output = output;
  }

  void myPrint(String text){
    lastTextPrinted = text;
    output.writeln(text);
  }
}
